package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {
    private List<PC> pcList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_layout);
        initPc();
        ListAdapter adapter=new ListAdapter(ListViewActivity.this,R.layout.list_item,pcList);
        ListView listView=(ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PC pc=pcList.get(position);
                String PCname=pc.getName();
                Intent intent=new Intent(ListViewActivity.this,ItemDetailActivity.class);
                intent.putExtra("电脑名称",PCname);
                startActivity(intent);
            }
        });
    }
    private void initPc(){
            PC a=new PC("1","联想","510947");
            pcList.add(a);
            PC b=new PC("2","戴尔","225193");
            pcList.add(b);
            PC c=new PC("3","华硕","222513");
            pcList.add(c);
            PC d=new PC("4","苹果","181891");
            pcList.add(d);
            PC e=new PC("5","惠普","91470");
            pcList.add(e);
            PC f=new PC("6","宏基","72139");
            pcList.add(f);
            PC g=new PC("7","索尼","64158");
            pcList.add(g);
            PC h=new PC("8","三星","52514");
            pcList.add(h);
            PC j=new PC("9","神舟","35647");
            pcList.add(j);
            PC k=new PC("10","东芝","20994");
            pcList.add(k);
    }
}